// gameBoard.ts
import { FlipCard } from './interfaces';
import { DeckManager } from './deckManager';

export class GameBoard {
  cards: (FlipCard | null)[] = Array(9).fill(null);

  setupInitialCards(deckManager: DeckManager): void {
    for (let i = 0; i < 9; i++) {
      if (this.cards[i] === null) {
        this.cards[i] = deckManager.drawCard();
      }
    }
  }

  flipCard(position: number): FlipCard | null {
    const card = this.cards[position];
    if (card) {
      this.cards[position] = null;
    }
    return card;
  }

  placeCard(position: number, card: FlipCard): void {
    this.cards[position] = card;
  }

  getAvailablePositions(): number[] {
    return this.cards
      .map((card, index) => card ? index : -1)
      .filter(index => index !== -1);
  }

  isEmpty(): boolean {
    return this.cards.every(card => card === null);
  }

  refillBoard(deckManager: DeckManager): void {
    for (let i = 0; i < 9; i++) {
      if (this.cards[i] === null) {
        this.cards[i] = deckManager.drawCard();
      }
    }
  }
}